﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;

namespace DAL
{
    public class HoaDon_DAL
    {
         private DataProvider _provider = new DataProvider();
	
		    public HoaDon_DAL()
		    {
			    _provider.connect();
		    }
        public void ThemHoaDon(HoaDon_DTO hd)
            {
                string insertCommand = @"INSERT INTO HOADON (MaHS, MaNV, TenHD, NgayHD, Trigia) VALUES(" +
                           hd.MaHS + ", " + hd.MaNV + ",'" + hd.TenHD + "','" + hd.NgayHD + "'," + hd.Trigia + ")";


                _provider.executeNonQuery(insertCommand);
            }
        public List<HoaDon_DTO> DS_HoaDon()
        {

            SqlDataReader reader = (SqlDataReader)_provider.executeQuery("DS_HoaDon");

            List<HoaDon_DTO> List_HD = new List<HoaDon_DTO>();

            while (reader.Read())
            {
                HoaDon_DTO hd = new HoaDon_DTO();
                hd.MaHD = int.Parse(reader["MaHD"].ToString());
                hd.MaHS = int.Parse(reader["MaHS"].ToString());
                hd.MaNV = int.Parse(reader["MaNV"].ToString());
                hd.TenHD = reader["TenHD"].ToString();
                hd.NgayHD=Convert.ToDateTime(reader["NgayHD"]);
                
                //hd.Trigia = Convert.ToInt32(reader["Trigia"].ToString()); 

                List_HD.Add(hd);

            }

            reader.Close();
            return List_HD;
        }

        public List<HoaDon_DTO> DS_HoaDonTheoMaHS(int MaHS)
        {
            // Một list các parameter
            List<SqlParameter> paramters = new List<SqlParameter>();
            paramters.Add(new SqlParameter("@MaHS", MaHS));



            // Gọi hàm excute sử dujg procedure có parameter
            SqlDataReader reader = (SqlDataReader)_provider.executeQueryParameter("DS_HoaDonTheoMaHS", paramters);

            List<HoaDon_DTO> List_HD = new List<HoaDon_DTO>();

            while (reader.Read())
            {
                HoaDon_DTO hd = new HoaDon_DTO();
                hd.MaHD = int.Parse(reader["MaHD"].ToString());
                hd.MaHS = int.Parse(reader["MaHS"].ToString());
                hd.MaNV = int.Parse(reader["MaNV"].ToString());
                hd.TenHD = reader["TenHD"].ToString();
                hd.NgayHD = Convert.ToDateTime(reader["NgayHD"]);
               
                //hd.Trigia = Convert.ToInt32(reader["Trigia"].ToString()); 

                List_HD.Add(hd);

            }

            reader.Close();
            return List_HD;
        }
    }
}
